public without sharing class CustomerRightsApplicationHandler {

	//对更新
	public static void excuteRight(List<CustomerRightsApplication__c> list_new  , Map<Id,CustomerRightsApplication__c> oldMap ){


		for (CustomerRightsApplication__c cra  : list_new ) {

			CustomerRightsApplication__c oldc = oldMap.get(cra.Id);

			if(cra.Status__c == '审批成功' && oldc.Status__c != '审批成功'  ){

				excuteRight(cra);
			}

		}
	}

	public static void  excuteRight(CustomerRightsApplication__c cra ){
		Map<Id,Boolean> accMap  = new Map<Id,Boolean >();
		Set<Id> oldOwnerSet   = new Set<Id >();
		Set<Id>accset = new Set<Id>();

		
		User u = [select Id ,FXProductLine__c from User where Id =:cra.User__c];
		if(cra.ProductLineNew__c != null )u.FXProductLine__c = cra.ProductLineNew__c ; 

		if(cra.ProductLineNew__c != null )update u ; 

		for (CustomerRightsDetails__c crd  :[select Account__c,Account__r.IFuserule__c,Account__r.OwnerId from CustomerRightsDetails__c where CustomerRightsApplication__c =:cra.Id ] ) {
				accMap.put(crd.Account__c,crd.Account__r.IFuserule__c);
				oldOwnerSet.add(crd.Account__r.OwnerId );
				if(cra.User__c != crd.Account__r.OwnerId )accset.add(crd.Account__c);
		}

		List<AccountTeamMember> atList = [select Id from AccountTeamMember where AccountId in:accMap.keySet() and UserId =: cra.User__c ];
		if(atList.size() > 0) delete atList ;

		if(cra.CustomerRightsType__c == '客户责任人转移'){
			accset.clear();
			List<Account> accList = new List<Account>();
			for (Id accid  :accMap.keySet()) {
				accList.add(new Account(Id =accid  , OwnerId=cra.User__c,IFuserule__c = !accMap.get(accid)));
			}
			if(accList.size()> 0) update accList ;
			accList.clear();
			Database.executeBatch(new CustomerRightsApplicationBatch(cra.Id ), 1) ;

			/*
			 //将nc客户信用的所有人也修改
            List<AccountCredit__c> acList = [select Id from AccountCredit__c where Account__c in: accMap.keySet() and OwnerId in: oldOwnerSet];
            for ( AccountCredit__c ac: acList ) {
            	ac.OwnerId = cra.User__c;
            }
            if(acList.size() > 0)update acList ;
            acList.clear(); 


			// 同时把活动的所有人也改了
			List<Event> eList = [select Id  from Event where Account__c in :accMap.keySet() and OwnerId in : oldOwnerSet];
			for (Event e : eList) {
				e.OwnerId = cra.User__c ;
			}
            if(eList.size() > 0 ) update eList;
            eList.clear();

			//将业务机会的所有人也修改
            List<Opportunity> oList = [select Id from Opportunity where AccountId in :accMap.keySet() and OwnerId in :oldOwnerSet and Is_Stop__c != true and StageName not in ('合同签订','合作确认','失败')  ];
            for(Opportunity o : oList){
            	o.OwnerId = cra.User__c ;
            } 
            if(oList.size() > 0) update oList;
            oList.clear();

            //将合同的所有人也修改
            List<Contract__c> cList = [select Id  from Contract__c where TerminalCustomer__c in :accMap.keySet()  and OwnerId in :oldOwnerSet and Status__c != '已生效'];
			for(Contract__c c : cList){
            	c.OwnerId = cra.User__c ;
            }
            if(cList.size() > 0) update cList;
            cList.clear();

            //将nc客户信用的所有人也修改
            List<InstallationApply__c> inList = [select Id from InstallationApply__c where Hospital__c in: accMap.keySet() and OwnerId in: oldOwnerSet and EndWorkOrderTimen__c = null  ];
            for ( InstallationApply__c ac: inList ) {
            	ac.OwnerId = cra.User__c ;
            }
            if(inList.size() > 0) update inList; 
            inList.clear(); */
                
		}else if(cra.CustomerRightsType__c == '客户团队权限共享'){
			accMap.clear(); 
			oldOwnerSet.clear(); 
			List<AccountTeamMember> accTeamList = new List<AccountTeamMember>();

			if(cra.OperationType__c == '新增共享权限'){

				for (Id accid  : accset) {
					accTeamList.add(new AccountTeamMember(UserId=cra.User__c,AccountId = accid ,TeamMemberRole='同客户多人负责'));
				}
				if(accTeamList.size() > 0 ) insert accTeamList; 

				List<AccountShare> ahList = new List<AccountShare>();
				for(AccountShare ah : [select Id , AccountAccessLevel ,CaseAccessLevel from AccountShare where AccountId in:accset and  RowCause = 'Team' and UserOrGroupId=:cra.User__c ] ){
					ah.AccountAccessLevel = 'Edit';
					ah.CaseAccessLevel = 'Read';

					ahList.add(ah);

				}
				if(ahList.size() > 0) update ahList;


			}else if(cra.OperationType__c == '删除共享权限'){

			}

		}

	}
    
}